// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.util.ArrayList;
import org.joml.Matrix3f;
import org.joml.Vector2f;
import org.joml.Vector4f;
import zombie.debug.DebugLog;
import zombie.iso.SpriteDetails.IsoFlagType;

public final class IsoWaterFlow {
    private static final ArrayList<Vector4f> points = new ArrayList();
    private static final ArrayList<Matrix3f> zones = new ArrayList();

    public static void addFlow(float x, float y, float flow, float speed) {
        int _int = (360 - (int)flow - 45) % 360;
        if (_int < 0) {
            _int += 360;
        }

        flow = (float)Math.toRadians((double)_int);
        points.add(new Vector4f(x, y, flow, speed));
    }

    public static void addZone(float x1, float y1, float x2, float y2, float shore, float water_ground) {
        if (x1 > x2 || y1 > y2 || (double)shore > 1.0) {
            DebugLog.log("ERROR IsoWaterFlow: Invalid waterzone (" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ")");
        }

        zones.add(new Matrix3f(x1, y1, x2, y2, shore, water_ground, 0.0F, 0.0F, 0.0F));
    }

    public static int getShore(int x, int y) {
        for (int _int = 0; _int < zones.size(); _int++) {
            Matrix3f matrix3f = (Matrix3f)zones.get(_int);
            if (matrix3f.m00 <= (float)x && matrix3f.m02 >= (float)x && matrix3f.m01 <= (float)y && matrix3f.m10 >= (float)y) {
                return (int)matrix3f.m11;
            }
        }

        return 1;
    }

    public static Vector2f getFlow(IsoGridSquare isoGridSquare0, int int2, int int1, Vector2f vector2f) {
        float float0 = 0.0F;
        float float1 = 0.0F;
        Vector4f vector4f0 = null;
        float float2 = Float.MAX_VALUE;
        Vector4f vector4f1 = null;
        float float3 = Float.MAX_VALUE;
        Vector4f vector4f2 = null;
        float float4 = Float.MAX_VALUE;
        if (points.size() == 0) {
            return vector2f.set(0.0F, 0.0F);
        } else {
            for (int int0 = 0; int0 < points.size(); int0++) {
                Vector4f vector4f3 = (Vector4f)points.get(int0);
                double double0 = Math.pow((double)(vector4f3.x - (float)(isoGridSquare0.x + int2)), 2.0)
                    + Math.pow((double)(vector4f3.y - (float)(isoGridSquare0.y + int1)), 2.0);
                if (double0 < (double)float2) {
                    float2 = (float)double0;
                    vector4f0 = vector4f3;
                }
            }

            for (int int3 = 0; int3 < points.size(); int3++) {
                Vector4f vector4f4 = (Vector4f)points.get(int3);
                double double1 = Math.pow((double)(vector4f4.x - (float)(isoGridSquare0.x + int2)), 2.0)
                    + Math.pow((double)(vector4f4.y - (float)(isoGridSquare0.y + int1)), 2.0);
                if (double1 < (double)float3 && vector4f4 != vector4f0) {
                    float3 = (float)double1;
                    vector4f1 = vector4f4;
                }
            }

            float2 = Math.max((float)Math.sqrt((double)float2), 0.1F);
            float3 = Math.max((float)Math.sqrt((double)float3), 0.1F);
            if (float2 > float3 * 10.0F) {
                float0 = vector4f0.z;
                float1 = vector4f0.w;
            } else {
                for (int int4 = 0; int4 < points.size(); int4++) {
                    Vector4f vector4f5 = (Vector4f)points.get(int4);
                    double double2 = Math.pow((double)(vector4f5.x - (float)(isoGridSquare0.x + int2)), 2.0)
                        + Math.pow((double)(vector4f5.y - (float)(isoGridSquare0.y + int1)), 2.0);
                    if (double2 < (double)float4 && vector4f5 != vector4f0 && vector4f5 != vector4f1) {
                        float4 = (float)double2;
                        vector4f2 = vector4f5;
                    }
                }

                float4 = Math.max((float)Math.sqrt((double)float4), 0.1F);
                float float5 = vector4f1.z * (1.0F - float3 / (float3 + float4)) + vector4f2.z * (1.0F - float4 / (float3 + float4));
                float float6 = vector4f1.w * (1.0F - float3 / (float3 + float4)) + vector4f2.w * (1.0F - float4 / (float3 + float4));
                float float7 = float3 * (1.0F - float3 / (float3 + float4)) + float4 * (1.0F - float4 / (float3 + float4));
                float0 = vector4f0.z * (1.0F - float2 / (float2 + float7)) + float5 * (1.0F - float7 / (float2 + float7));
                float1 = vector4f0.w * (1.0F - float2 / (float2 + float7)) + float6 * (1.0F - float7 / (float2 + float7));
            }

            float float8 = 1.0F;
            IsoCell isoCell = isoGridSquare0.getCell();

            for (int int5 = -5; int5 < 5; int5++) {
                for (int int6 = -5; int6 < 5; int6++) {
                    IsoGridSquare isoGridSquare1 = isoCell.getGridSquare(isoGridSquare0.x + int2 + int5, isoGridSquare0.y + int1 + int6, 0);
                    if (isoGridSquare1 == null || !isoGridSquare1.getProperties().Is(IsoFlagType.water)) {
                        float8 = (float)Math.min((double)float8, Math.max(0.0, Math.sqrt((double)(int5 * int5 + int6 * int6))) / 4.0);
                    }
                }
            }

            float1 *= float8;
            return vector2f.set(float0, float1);
        }
    }

    public static void Reset() {
        points.clear();
        zones.clear();
    }
}
